package br.edu.universo.si.projeto2.infojob.client.apresentacao;

import java.util.ArrayList;

import br.edu.universo.si.projeto2.infojob.client.bean.BeanProfissionalCurriculo;
import br.edu.universo.si.projeto2.infojob.client.bean.BeanVaga;
import br.edu.universo.si.projeto2.infojob.client.componentes.dialogboxes.ErrorDialogBox;
import br.edu.universo.si.projeto2.infojob.client.componentes.dialogboxes.InformationDialogBox;
import br.edu.universo.si.projeto2.infojob.client.componentes.dialogboxes.SuccessDialogBox;
import br.edu.universo.si.projeto2.infojob.client.util.Util;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

public class AEnviaCurriculo extends DialogBox
{
	private FlexTable flexTable;

	private ListBox curriculosCb;

	private Button enviarBtn;
	private Button cancelarBtn;

	public AEnviaCurriculo(final BeanVaga vaga)
	{
		super(false, true);

		this.setAnimationEnabled(true);
		this.setText("Candidatar a vaga!");

		flexTable = new FlexTable();
		curriculosCb = new ListBox(false);
		enviarBtn = new Button("Enviar");
		cancelarBtn = new Button("Cancelar");

		curriculosCb.setWidth("450px");

		flexTable.setWidget(0, 0, new HTML("<b>Escolha o currículo a ser enviado:</b>"));
		flexTable.getFlexCellFormatter().setAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(1, 0, curriculosCb);
		flexTable.getFlexCellFormatter().setAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);

		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.setSpacing(10);
		hPanel.add(enviarBtn);
		hPanel.add(cancelarBtn);

		flexTable.setWidget(2, 0, hPanel);
		flexTable.getFlexCellFormatter().setAlignment(2, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);

		this.add(flexTable);

		// fecha a janela
		cancelarBtn.addClickListener(
			new ClickListener()
			{
				public void onClick(Widget sender)
				{
					AEnviaCurriculo.this.hide();
				}
			}
		);


		enviarBtn.addClickListener(
			new ClickListener()
			{
				public void onClick(Widget sender)
				{
					AsyncCallback cbEnviarCurriculo = new AsyncCallback()
					{
						public void onSuccess(Object result)
						{
							AEnviaCurriculo.this.hide();

							SuccessDialogBox successDialogBox = new SuccessDialogBox("Currículo enviado com sucesso!");
							successDialogBox.center();
							successDialogBox.show();
						};

						public void onFailure(Throwable caught)
						{
							ErrorDialogBox errorDialogBox = new ErrorDialogBox("Erro ao enviar currículo: " + caught.getMessage());
							errorDialogBox.center();
							errorDialogBox.show();
						};
					};

					BeanProfissionalCurriculo bCurriculo = new BeanProfissionalCurriculo();
					bCurriculo.setId(Integer.parseInt(curriculosCb.getValue(curriculosCb.getSelectedIndex())));

					Util.getService().inserirCandidato(vaga, bCurriculo, cbEnviarCurriculo);
				}
			}
		);

		AsyncCallback<ArrayList<BeanProfissionalCurriculo>> curriculosCallback = new AsyncCallback<ArrayList<BeanProfissionalCurriculo>>()
		{
			public void onSuccess(ArrayList<BeanProfissionalCurriculo> clCurriculos)
			{
				// se nao existirem curriculos cadastrados, fecha a tela
				if (clCurriculos.size() <= 0)
				{
					AEnviaCurriculo.this.hide();

					InformationDialogBox infoDialogBox = new InformationDialogBox("Não existe nenhum currículo cadastrado.");
					infoDialogBox.center();
					infoDialogBox.show();
				}

				for (BeanProfissionalCurriculo bCurriculo : clCurriculos)
				{
					curriculosCb.addItem(bCurriculo.getComentario(), Integer.toString(bCurriculo.getId()));
				}
			}

			public void onFailure(Throwable caught)
			{
				ErrorDialogBox errorDialogBox = new ErrorDialogBox("Erro ao buscar currículos cadastrados: " + caught.getMessage());
				errorDialogBox.center();
				errorDialogBox.show();
			}
		};

		Util.getService().getCurriculos(curriculosCallback);
	}
}